package pandabay2.DAO;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;

import pandabay2.domain.User;
import pandabay2.service.ConnectionManager;

public class UserDAO {
	
	static Connection currentCon = null;
	static ResultSet rs = null;

	public static User login(User bean) {
		Statement stmt = null;
		String email = bean.getEmail();
		String password = bean.getPassword();
		String searchQuery = "select * from user where email='" + email + "' AND password='" + password + "'";
		
		try {
			//connecting to the DB
			currentCon = ConnectionManager.getConnection();
			stmt=currentCon.createStatement();
			rs = stmt.executeQuery(searchQuery);
			boolean userExists = rs.next();
			
			if (!userExists) {
				System.out.println("Username/Password entered is Incorrect or User does not Exists.");
				bean.setValid(false);
			} else if (userExists) {
				String firstName = rs.getString("FirstName");
				String lastName = rs.getString("LastName");
				String role = rs.getString("role");
				int user_id = rs.getInt("user_id");
				System.out.println("Welcome " + firstName);
				bean.setFirstName(firstName);
				bean.setLastName(lastName);
				bean.setUser_id(user_id);
				bean.setRole(role);
				bean.setValid(true);
			}	
		} catch (Exception ex) {
			System.out.println("Log In failed: An Exception has occurred! " + ex);
		}
		return bean;
	}
	
	public static User register(User bean) {
		Statement stmt = null;
		String email = bean.getEmail();
		String password = bean.getPassword();
		String firstName = bean.getFirstName();
		String lastName = bean.getLastName();
		String street = bean.getStreet();
		String role = bean.getRole();
		int plz = bean.getPlz();
		String city = bean.getCity();
		String searchQuery = "select * from user where email='" + email + "'";
		//String registerQuery = "INSERT INTO user (user_id, firstName, lastName, email, password, street, plz, city) VALUES (2, '" + firstName + "','" + lastName + "','" + email + "','" + password + "','" + street + "','" + plz + "','" + city + "'";
		String registerQuery = "INSERT INTO user (firstName, lastName, email, password, street, plz, city, role) VALUES ('" + firstName + "','" + lastName + "','" + email + "','" + password + "','" + street + "'," + plz + ",'" + city + "','" + role +"')";
		
		try {
			
			//connecting to the DB
			currentCon = ConnectionManager.getConnection();
			stmt=currentCon.createStatement();
			rs = stmt.executeQuery(searchQuery);
			boolean userExists = rs.next();
			
			if (userExists) {
				System.out.println("Username already registered!.");
				bean.setValid(false);
			} else if (!userExists) {
				stmt.executeUpdate(registerQuery);
				System.out.println("Welcome " + firstName);
				//int user_id = rs.getInt("user_id");
				//bean.setUser_id(user_id);
				bean.setValid(true);
			}	
		} catch (Exception ex) {
			System.out.println("Registration failed: An Exception has occurred! " + ex);
		}
		return bean;
	}
	
	public static void updateUser(String email, String password, String firstName, String lastName, String street, String city, int plz, int user_id) {
		Statement stmt = null;
		String searchQuery = "select * from user where user_id='" + user_id + "'";
		String updateQuery = "UPDATE user SET firstname='"+ firstName + "',lastName='" + lastName + "',email='" + email + "',password='" + password + "',street='" + street + "',plz=" + plz + ",city='" + city + "' WHERE user_id =" + user_id + "";
		
		try {
			//connecting to the DB
			currentCon = ConnectionManager.getConnection();
			stmt=currentCon.createStatement();
			rs = stmt.executeQuery(searchQuery);
			boolean userExists = rs.next();
			
			if (userExists) {
				stmt.executeUpdate(updateQuery);
				System.out.println("Saved Profile: " + firstName);
			} else if (!userExists) {
				System.out.println("Username is not registered!");
			}	
		} catch (Exception ex) {
			System.out.println("Profile editing failed: An Exception has occurred! " + ex);
		}
	}

	public static User findUser(int seller) {
		Statement stmt = null;
		User bean = null;
		String searchQuery = "SELECT * FROM user WHERE user_id='" + seller + "'";
		
		try {
			//connecting to the DB
			currentCon = ConnectionManager.getConnection();
			stmt=currentCon.createStatement();
			rs = stmt.executeQuery(searchQuery);
			boolean userExists = rs.next();
			
			if (!userExists) {
				System.out.println("User not found");
				bean.setValid(false);
			} else if (userExists) {
				String firstName = rs.getString("firstName");
				String lastName = rs.getString("lastName");
				String email = rs.getString("email");
				//String role = rs.getString("role");
				//List<Integer> rating = (List<Integer>) rs.getArray("rating");
				int user_id = rs.getInt("user_id");
				System.out.println("found: " + firstName);
				bean.setFirstName(firstName);
				bean.setLastName(lastName);
				bean.setEmail(email);
				bean.setUser_id(user_id);
				//bean.setRole(role);
				bean.setValid(true);
				//bean.setRating(rating);
				return bean;
			}	
		} catch (Exception ex) {
			System.out.println("Search failed: An Exception has occurred! " + ex);
		}
		return bean;
	}
	
	public static void updateRating(int user_id, int currentRating) {
		Statement stmt = null;
		List<Integer> rating = null;
		String searchQuery = "select rating from user where user_id='" + user_id + "'";
		
		try {
			//connecting to the DB
			currentCon = ConnectionManager.getConnection();
			stmt=currentCon.createStatement();
			rs = stmt.executeQuery(searchQuery);
			//rating = (List<Integer>) rs.getArray("rating");
			//rating.add(currentRating);
			boolean userExists = rs.next();
			
			if (userExists) {
				String updateQuery = "UPDATE user SET rating='"+ rating + "'";
				stmt.executeUpdate(updateQuery);
				System.out.println("Added rating: " + rating);
			} else if (!userExists) {
				System.out.println("rating failed");
			}	
		} catch (Exception ex) {
			System.out.println("Rating failed: An Exception has occurred! " + ex);
		}
	}
}
